package com.lijy.superseckill.service;

import com.lijy.superseckill.dao.SeckillUserDao;
import com.lijy.superseckill.domain.SeckillUser;
import com.lijy.superseckill.exception.GlobalException;
import com.lijy.superseckill.result.CodeMsg;
import com.lijy.superseckill.util.MD5Util;
import com.lijy.superseckill.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SeckillUserService {
    @Autowired
    private SeckillUserDao seckillUserDao;

    public SeckillUser getById(Long id) {
        return seckillUserDao.getById(id);
    }

    public boolean login(LoginVO loginVO) {
        if (loginVO == null) {
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        String mobile = loginVO.getMobile();
        String formPass = loginVO.getPassword();
        // 判断手机号是否存在
        SeckillUser user = getById(Long.parseLong(mobile));
        if (user == null) {
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        // 验证密码
        String dbPass = user.getPassword();
        String saltDB = user.getSalt();
        String secondMd5 = MD5Util.secondMd5(formPass, saltDB);
        if (!secondMd5.equals(dbPass)) {
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }
        return true;

    }
}
